package co.wangdao.xingshaoye.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import co.wangdao.xingshaoye.view.activity.CkdetailsActivity;
import co.wangdao.xingshaoye.R;
import co.wangdao.xingshaoye.bean.ShopMessage;
import co.wangdao.xingshaoye.bean.User;
import co.wangdao.xingshaoye.utils.CommonAdapter;
import co.wangdao.xingshaoye.utils.ViewHolder;
import co.wangdao.xingshaoye.view.activity.SearchActivity;
import co.wangdao.xingshaoye.widget.RefreshableView;

public class Fragment1 extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener{

	private Activity activity;
	private View layout;
	private List<ShopMessage> shopMessageList; //从接口获取的首页店铺信息
	private RefreshableView refreshableView;




	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null){
			activity = this.getActivity();
		}

		layout = inflater.inflate(R.layout.layout_bus_listview, null); //先解析file.xml布局，得到一个view
		ListView listView = (ListView) layout.findViewById(R.id.msg_list_view);




		//测试数据
		initMessagetest();

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Adapter adapter = arg0.getAdapter();
				ShopMessage shopMessage = (ShopMessage) adapter.getItem(position);

				Toast.makeText(activity,shopMessage.getUid(),Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(getActivity(),CkdetailsActivity.class);
				intent.putExtra("uid",shopMessage.getUid());

				startActivity(intent);
			}
		});

		//initMessage();

		listView.setAdapter(new CommonAdapter<ShopMessage>(activity, shopMessageList, R.layout.layout_business_items) {
			@Override
			public void convert(ViewHolder helper, ShopMessage item) {
				helper.setImageByUrl(getContext(), R.id.bus_item_img, item.getLogo());
				helper.setImageByUrl(getContext(), R.id.face_img, item.getUserinfo().getAvatar256());
				helper.setText(R.id.shopname, item.getTitle());
				helper.setText(R.id.map_title, item.getMap_title());
				helper.setText(R.id.opening_time, item.getShop_hours_start() + "-" + item.getShop_hours_end());
				helper.setText(R.id.juli, item.getGaodejuli());
//				if (item.getOperat_status().equals("0")){
//					helper.setImageResource(R.id.yingyestate,R.drawable.yingye_0);
//				}


			}
		});

		refreshableView = (RefreshableView) layout.findViewById(R.id.shoprefresh);
		refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
			@Override
			public void onRefresh() {
				try {
					Thread.sleep(3000);
					initMessagetest();
				} catch (Exception e) {
					e.printStackTrace();
				}
				refreshableView.finishRefreshing();
			}
		},0);

		return layout;
	}


	private void initMessagetest(){

		shopMessageList = new ArrayList<ShopMessage>();

		User user1 = new User();
		user1.setAvatar256("http://web1.app.ijiaque.com/Public/images/default_avatar_thumb_256_256.jpg");
		ShopMessage shopMessage1 = new ShopMessage();
		shopMessage1.setUid("2090");
		shopMessage1.setLogo("http://web1.app.ijiaque.com/Uploads/Picture/2016-01-29/56ab1201655f4_thumb_360_220.jpg");
		shopMessage1.setUserinfo(user1);
		shopMessage1.setTitle("陕八怪");
		shopMessage1.setSignature("陕八怪123");
		shopMessage1.setShop_hours_start("8:00");
		shopMessage1.setShop_hours_end("22:00");
		shopMessage1.setMap_title("山东省济南市历下区姚家街道山东政法学院民商法学院（解放东路）");
		shopMessage1.setGaodejuli("3.6km");
		shopMessageList.add(shopMessage1);


		User user2 = new User();
		user1.setAvatar256("http://web1.app.ijiaque.com/Public/images/default_avatar_thumb_256_256.jpg");
		ShopMessage shopMessage2 = new ShopMessage();
		shopMessage2.setLogo("http://web1.app.ijiaque.com/Uploads/Picture/2016-01-29/56ab1201655f4_thumb_360_220.jpg");
		shopMessage2.setUserinfo(user2);
		shopMessage2.setTitle("陕9怪");
		shopMessage2.setSignature("陕八怪123");
		shopMessage2.setShop_hours_start("8:00");
		shopMessage2.setShop_hours_end("22:00");
		shopMessage2.setMap_title("山东省济南市历下区姚家街道山东政法学院民商法学院（解放东路）");
		shopMessage2.setGaodejuli("100km");
		shopMessageList.add(shopMessage2);

	}

	private void initMessage(){
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("page", "1");
		params.put("map_title", "济南");
		params.put("coord", "117.055614,36.666577");
		client.post("http://web1.app.ijiaque.com/app/demo/shopslist.html", params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(JSONObject response) {
						System.out.print(response);
//						ShopMessage ShopMessage = gson.fromJson(response, ShopMessage.class);
//						shopMessageList.add(ShopMessage);
						super.onSuccess(response);
					}

					@Override
					public void onFailure(int statusCode, Throwable e, JSONArray errorResponse) {
						super.onFailure(statusCode, e, errorResponse);
					}

					String status= null;


				});
	}


	@Override
	public void onClick(View v) {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Adapter adapter = parent.getAdapter();
		ShopMessage shopMessage = (ShopMessage) adapter.getItem(position);

		Toast.makeText(activity,shopMessage.getUid(),Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(getActivity(),CkdetailsActivity.class);
		intent.putExtra("uid",shopMessage.getUid());

		startActivity(intent);


	}
}
